package subraSoft.fenix.model.authentication.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;

import subraSoft.centaurus.model.domain.CenDefaultDO;
import subraSoft.centaurus.model.service.impl.CenDefaultServiceImpl;
import subraSoft.fenix.model.authentication.cobj.FenObjSecCObj;
import subraSoft.fenix.model.authentication.domain.FenObjSecDO;
import subraSoft.fenix.model.authentication.service.FenObjSecService;

public class FenObjSecServiceImpl extends CenDefaultServiceImpl 
	implements FenObjSecService 
{
	@SuppressWarnings("unchecked")
	public List<FenObjSecCObj> getList() 
	{
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		
		List<FenObjSecCObj> res = new ArrayList<FenObjSecCObj>();
		Criteria c = session.createCriteria(FenObjSecDO.class);
		List<CenDefaultDO> objetosSec = c.list();
		
		for (CenDefaultDO objetoSecDO : objetosSec) 
		{
			res.add(getCObj((FenObjSecDO) objetoSecDO));
		}
		
		session.getTransaction().commit();
		return res;
	}

	public FenObjSecCObj getCObj(FenObjSecDO objetoSecDO) {
		
		FenObjSecCObj cObj = new FenObjSecCObj();
		cObj.setId(objetoSecDO.getId());
		cObj.setDescripcion(objetoSecDO.getDescripcion());
		return cObj;
	}

	@SuppressWarnings("unchecked")
	public List<FenObjSecCObj> getObjetosSeguidad(String idUsr)
	{
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		
		Query query = session.createQuery("select sec " +
				"from FenUsuarioDO user," +
				"FenPerfilDO perfilT join perfilT.objetosSeg as objetosSeguridad," +
				"FenObjSecDO sec " +
				"where user.perfil.id = perfilT.id " +
				"and user.id = '" + idUsr + "'" +
				"and sec.id in (objetosSeguridad.id)");
	    
		List<FenObjSecCObj> res = new ArrayList<FenObjSecCObj>();
		List<FenObjSecDO> results = query.list();
		
		for (FenObjSecDO fenObjSecDO : results) {
			res.add(getCObj(fenObjSecDO));
		}
	    
		session.getTransaction().commit();
		
		return res;
	}
}
